import {Component, EventEmitter,Input, OnInit, Output} from "@angular/core";
import {NzNotificationService} from "ng-zorro-antd";
import {HttpServcie} from "../../../../service/http.service";
import {SpotObjectPlan} from "../../bean/spotObjectPlan";

/**
 * 下达任务的ts
 */


@Component({
  selector: "spot-object-modify",
  templateUrl: "spot-object-modify.html",
  styleUrls: ["spot-object-modify.scss"]
})
export class SpotObjectModify implements OnInit {

  @Input()
  inValue: any = {};

  // 定义父子传值事件
  @Output("outPut")
  public outPut = new EventEmitter();
  // 定义对象
  public spotObjectPlan:SpotObjectPlan = new SpotObjectPlan();
  // 定义当前登录人的信息
  public user:any;
  // 定义实施人和责任人的信息
  public objects:any=[];
  // 定义实施对象选中数据
  public objectList:any=[];
  // 定义计划名称
  public planNameEn:any;
  // 定义计划实体信息
  public planData:any;
  // 定义选择的数据集合
  public updateList:any=[];
  // 定义修改条数数据Id
  public updateId:any=[];
  // 定义检查人员
  public checkMan:any=[];

  constructor(
    private _notification: NzNotificationService,
    private httpServcie: HttpServcie
  ) {
  }

  ngOnInit() {
    // 先情况选择项
    this.user = JSON.parse(window.localStorage.getItem("user"));

    this.planNameEn = this.inValue.planDict.planName;
    this.updateList = this.inValue.updateList;

    this.updateList.forEach(element => {
      this.objectList.push(element.implementObject);

      this.updateId.push(element.id);
    });

    // 单位下的所有维保人员
    this.queryMachine();
  }

  // 修改方法检查人员
  submitForm($event) {

    let updateId:string = this.updateId.join(',');
    let checkMan = this.checkMan;

    this.httpServcie.insertCheckMan({"updateId":updateId,"checkMan":checkMan}).subscribe(data => {
        if (data.status == 200) {
          this._notification.create("success", "成功", "新增成功！");
          this.outPut.emit($event);
        } else {
          this._notification.create("error", "错误提示", data.message);
        }
      },
      error => {
        this._notification.create("error", "错误提示", error);
      }
    )
  }

  // 根据当前得维保单位查询维保人员
  queryMachine() {

    let obj:any =[]
    this.httpServcie.queryUserByPartyId(this.user.partyId).subscribe(data => {
      if (data.status == 200) {
        if (data.data && data.data.length > 0) {
          for (let i = 0; i < data.data.length; i++) {
            if (data.data[i].roleId == '17') {
              obj.push(data.data[i]);
            }
          }
        }
      }
      this.objects =  obj;
    });
  }


}
